/**
 * Copyright &copy; 2012-2014 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.sys.utils;

import com.google.common.collect.Maps;
import com.thinkgem.jeesite.common.utils.CacheUtils;
import com.thinkgem.jeesite.common.utils.SpringContextHolder;
import com.thinkgem.jeesite.modules.book.dao.BookTypeDao;
import com.thinkgem.jeesite.modules.book.entity.BkType;
import org.apache.commons.lang3.StringUtils;

import java.util.Map;

/**
 * 字典工具类
 * @author ThinkGem
 * @version 2013-5-29
 */
public class BkTypeUtils{
	
	private static BookTypeDao bookTypeDao = SpringContextHolder.getBean(BookTypeDao.class);

	public static final String CACHE_BOOKTYPE_MAP = "bookTypeMap";

	public static String getTypeName(String name){
		if(StringUtils.isNotBlank(name)){
			BkType type = getType(name);
			return type.getTypDisn();
		}else{
			return "";
		}
	}

	public static BkType getType(String name){
		@SuppressWarnings("unchecked")
		Map<String, BkType> bkTypeMap = (Map<String, BkType>)CacheUtils.get(CACHE_BOOKTYPE_MAP);
		if(bkTypeMap == null){
			bkTypeMap = Maps.newHashMap();
			for(BkType type : bookTypeDao.getAll()){
				bkTypeMap.put(type.getTypName(), type);
			}
			CacheUtils.put(CACHE_BOOKTYPE_MAP, bkTypeMap);
		}
		BkType type = bkTypeMap.get(name);
		if (type == null){
			type = new BkType();
		}
		return type;
	}
}
